﻿using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;

namespace EventBus.Models
{
    public class StudentHandler
        : ILocalEventHandler<ScoreCountChangedEvent>,ILocalEventHandler<EntityCreatedEventData<Student>>, ILocalEventHandler<EntityCreatingEventData<Student>>
          , ITransientDependency
    {

       public IScoreService scoreService { get; set; }
        public async Task HandleEventAsync(ScoreCountChangedEvent eventData)
        {
            //TODO: your code that does somthing on the event

           await scoreService.AddScoreAsync(eventData.StudentId, eventData.Score);
            Console.WriteLine("学生积分订阅完成");
        }

        //当实体写入数据库之后执行
        public async Task HandleEventAsync(EntityCreatedEventData<Student> eventData)
        {
            await scoreService.AddScoreAsync(eventData.Entity.Id, 10);
            Console.WriteLine("实体写入成功后执行");
        }
        /// <summary>
        /// 实体写入数据库之前执行
        /// </summary>
        /// <param name="eventData"></param>
        /// <returns></returns>
        public async Task HandleEventAsync(EntityCreatingEventData<Student> eventData)
        {
            await scoreService.AddScoreAsync(eventData.Entity.Id, 10);
            Console.WriteLine("实体写入前执行");
        }
    }
}


